﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using VehicleMaintenance.Framework;
using VehicleMaintenance.Models;

namespace VehicleMaintenance.BaseInfo
{
	public partial class MaintenanceCategoriesForm : ListAndViewBaseForm
	{
		private MaintenanceCategories category;

		public MaintenanceCategories Category
		{
			get { return category; }
			set { category = value; }
		}


		public MaintenanceCategoriesForm()
		{
			InitializeComponent();
			ucFormInput1.InitializeControls(typeof(Models.MaintenanceCategories));
		}

		public MaintenanceCategoriesForm(bool select)
		{
			InitializeComponent();
			ucFormInput1.InitializeControls(typeof(Models.MaintenanceCategories));
			isSelect = select;
		}

		private void MaintenanceCategoriesForm_Load(object sender, EventArgs e)
		{
			try
			{
				BindData();
			}
			catch (Exception ex)
			{
				this.ShowInfo(ex.Message);
			}
		}

		private void btnEdit_Click(object sender, EventArgs e)
		{
			try
			{
				if (lvData.SelectedItems.Count > 0)
				{
					this.panel2.Enabled = true;
				}
			}
			catch (Exception ex)
			{
				this.ShowInfo(ex.Message);
			}
		}

		private void lvData_SelectedIndexChanged(object sender, EventArgs e)
		{
			try
			{
				if (lvData.SelectedItems.Count > 0)
				{
					this.panel2.Enabled = false;
					this.state = State.Edit;

					CarDBContext db = new CarDBContext();
					int id = Convert.ToInt32(lvData.SelectedItems[0].Tag);
					MaintenanceCategories category = db.MaintenanceCategories.FirstOrDefault(p => p.CategoryID == id);
					if (category != null)
					{
						ucFormInput1.SetValues(category);
					}

				}
			}
			catch (Exception ex)
			{
				this.ShowInfo(ex.Message);
			}

		}

		private void btnDelete_Click(object sender, EventArgs e)
		{
			try
			{
				if (lvData.SelectedItems.Count > 0)
				{
					if (MessageBox.Show(this, "是否删除当前维修类型？", "提示", MessageBoxButtons.YesNo) == System.Windows.Forms.DialogResult.Yes)
					{
						CarDBContext db = new CarDBContext();

						int id = Convert.ToInt32(lvData.SelectedItems[0].Tag);
						db.MaintenanceCategories.Remove(db.MaintenanceCategories.First(p => p.CategoryID == id));

						db.SaveChanges();

						BindData();
						ucFormInput1.ClearData();
					}
				}
			}
			catch (Exception ex)
			{
				this.ShowInfo(ex.Message);
			}
		}

		private void btnSave_Click(object sender, EventArgs e)
		{
			try
			{
				CarDBContext db = new CarDBContext();

				if (this.state == State.Edit)
				{
					int id = Convert.ToInt32(lvData.SelectedItems[0].Tag);
					MaintenanceCategories category = db.MaintenanceCategories.First(p => p.CategoryID == id);

					ucFormInput1.GetValues(category);

					db.SaveChanges();
					BindData();
					this.panel2.Enabled = false;

					this.ShowInfo("保存成功！");
				}
				else
				{
					var category = db.MaintenanceCategories.Create();
					ucFormInput1.GetValues(category);

					db.MaintenanceCategories.Add(category);
					db.SaveChanges();
					BindData();

					if (MessageBox.Show(this, "保存成功，是否继续添加？", "提示", MessageBoxButtons.YesNo) == System.Windows.Forms.DialogResult.Yes)
					{
						this.panel2.Enabled = true;
						this.state = State.Add;
						ucFormInput1.ClearData();

						ucFormInput1.GetControlByName("Name").Focus();
					}
					else
					{
						this.panel2.Enabled = false;
						this.state = State.Add;
						ucFormInput1.ClearData();
					}
				}
			}
			catch (Exception ex)
			{
				this.ShowInfo(ex.Message);
			}
		}

		private void lvData_DoubleClick(object sender, EventArgs e)
		{
			try
			{
				if (lvData.SelectedItems.Count > 0)
				{
					if (isSelect)
					{
						CarDBContext db = new CarDBContext();

						int id = Convert.ToInt32(lvData.SelectedItems[0].Tag);
						Category = db.MaintenanceCategories.First(p => p.CategoryID == id);

						this.DialogResult = System.Windows.Forms.DialogResult.OK;
					}
					else
					{
						btnEdit_Click(sender, e);
					}
				}
			}
			catch (Exception ex)
			{
				this.ShowInfo(ex.Message);
			}
		}


		private void BindData()
		{
			try
			{
				lvData.Items.Clear();

				CarDBContext db = new CarDBContext();
				var list = db.MaintenanceCategories;

				foreach (var item in list)
				{
					ListViewItem lvItem = new ListViewItem(new string[] { item.Name, item.Remark });
					lvItem.Tag = item.CategoryID;
					lvData.Items.Add(lvItem);
				}
			}
			catch (Exception ex)
			{
				throw ex;
			}
		}



	}
}

